<?php

/**
 * Script used to fetch all projects in the database. If the calling function
 * wished to fetch whether or not the employee has viewed the projects before/
 * (posted 'pw'), then fetch all viewed projects too.
 */

// Start the session handling system
session_start ();

// Connect to the database
require_once ("../../db.php");

// Only allow this for users that are logged in
if (!isset($_SESSION['uid']))
    die('You are not logged in');

$sql = 'SELECT p.*, s.viewed, s.comment FROM projects p 
LEFT JOIN staffcomments s
ON s.uid = "' .$_SESSION['uid'] . '" AND s.projectid = p.id';
$sth = $db->prepare($sql);
$sth->execute();
$row = $sth->fetchAll();


if($row){
    echo json_encode($row);
} else {
    echo json_encode(array('error' => 'Fant ingen prosjekter.'));
}

?>